﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class AdvanceSearch: IAdvanceSearch
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public AdvanceSearch(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.AdvanceSearch GetAdvanceSearch(System.Int32 searchID)
		{
			BEGDC.PreContract.DAL.AdvanceSearch advanceSearch = BEGDC.PreContract.DAL.AdvanceSearch.TryFind(searchID);

            return advanceSearch;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetAdvanceSearch_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM AdvanceSearch";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteAdvanceSearch(System.Int32 searchID)
		{
			try
            {
				BEGDC.PreContract.DAL.AdvanceSearch advanceSearch = BEGDC.PreContract.DAL.AdvanceSearch.TryFind(searchID);
                advanceSearch.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("AdvanceSearch", string.Format("Error occured when deleting the AdvanceSearch, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddAdvanceSearch(BEGDC.PreContract.DAL.Employee employeeID, System.String searchType, System.String searchName, System.String description, System.String searchSQL, System.String searchCondition, System.String searchField)	
		{
			try
            {
				BEGDC.PreContract.DAL.AdvanceSearch advanceSearch = new BEGDC.PreContract.DAL.AdvanceSearch();
				
				advanceSearch.EmployeeID = employeeID;
				advanceSearch.SearchType = searchType;
				advanceSearch.SearchName = searchName;
				advanceSearch.Description = description;
				advanceSearch.SearchSQL = searchSQL;
				advanceSearch.SearchCondition = searchCondition;
				advanceSearch.SearchField = searchField;
				advanceSearch.CreateAndFlush();
				
				return advanceSearch.SearchID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("AdvanceSearch", string.Format("Error occured when adding a new AdvanceSearch, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdateAdvanceSearch(System.Int32 searchID, BEGDC.PreContract.DAL.Employee employeeID, System.String searchType, System.String searchName, System.String description, System.String searchSQL, System.String searchCondition, System.String searchField)
		{
			try
            {
                BEGDC.PreContract.DAL.AdvanceSearch advanceSearch = GetAdvanceSearch(searchID);

				advanceSearch.EmployeeID = employeeID;
				advanceSearch.SearchType = searchType;
				advanceSearch.SearchName = searchName;
				advanceSearch.Description = description;
				advanceSearch.SearchSQL = searchSQL;
				advanceSearch.SearchCondition = searchCondition;
				advanceSearch.SearchField = searchField;
				advanceSearch.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("AdvanceSearch", string.Format("Error occured when updating the AdvanceSearch, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



